package middlewares

import (
	"fmt"
	"hello/app/http/controllers"
	"hello/app/models"
	"net/http"
)

func AdminAuth(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		session, err := models.Admin{}.GetAdminBySession(r)
		base := controllers.Base{}
		if err != nil {
			base.ErrorJson(w, "", err.Error())
		}
		authenticated, authok := session.Values["authenticated"]
		if !authok || authenticated == nil {
			//url := grouter.Name2URL("adminlogin")todo
			//http.Redirect(w, r, url, http.StatusFound)todo
		}
		fmt.Println("通过了auth中间件")
		// 2. 继续处理接下去的请求
		next.ServeHTTP(w, r)
	})
}
